package com.tzsp.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tzsp.reggie.commons.dto.DishDto;
import com.tzsp.reggie.commons.utils.BusinessException;
import com.tzsp.reggie.domain.Dish;
import com.tzsp.reggie.domain.DishFlavor;
import com.tzsp.reggie.mapper.DishMapper;
import com.tzsp.reggie.service.DishFlavorService;
import com.tzsp.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {


    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品的同时，保存对应的口味数据
     * 由于同时操作2张表，所以需要添加事务
     *
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);

        //菜品id
        Long dishId = dishDto.getId();

        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品的口味信息到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id获取菜品和菜品的口味，展示在修改菜品页面
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //获取dish菜品信息
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();

        BeanUtils.copyProperties(dish,dishDto);

        //获取dish_flavors菜品信息
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> dishFlavors = dishFlavorService.list(queryWrapper);


        dishDto.setFlavors(dishFlavors);
        return dishDto;
    }

    /**
     * 修改菜品和菜品口味
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish菜品表
        this.updateById(dishDto);

        //删除dish_flavors
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        //获取菜品口味信息(只能从前端获取到name和value属性)
        List<DishFlavor> flavors = dishDto.getFlavors();
        //将DishId赋值给到flavors（里面有item，item中有DishId）
        flavors = flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        //新增dish_flavors
        dishFlavorService.saveBatch(flavors);
    }
    @Transactional
    @Override
    public void deleteWithFlavor(List<Long> ids) {
        //删除菜品
        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
        dishQueryWrapper.in(ids!=null,Dish::getId,ids);
        List<Dish> dishList = this.list(dishQueryWrapper);
        if (dishList!=null){
            for(Dish dish:dishList){
                if (dish.getStatus() == 0){
                    this.remove(dishQueryWrapper);
                }else {
                    throw new BusinessException("有菜品正在售卖，无法全部删除！");
                }
            }
        }
        //删除菜品口味
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ids!=null,DishFlavor::getDishId,ids);
        List<DishFlavor> dishFlavorList = dishFlavorService.list(queryWrapper);
        if (dishFlavorList!=null){
           dishFlavorService.remove(queryWrapper);
        }

    }
}
